package com.example.xm.service.impl;

import com.example.xm.entity.*;
import com.example.xm.entity.dto.OrderDto;
import com.example.xm.entity.dto.OrderItemDto;
import com.example.xm.entity.dto.OrderListDto;
import com.example.xm.entity.vo.OrderItemListVo;
import com.example.xm.entity.vo.OrderListVo;
import com.example.xm.mapper.*;
import com.example.xm.service.OrderService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author tong
* @description 针对表【order】的数据库操作Service实现
* @createDate 2024-04-13 12:37:40
*/
@Service
public class OrderServiceImpl implements OrderService {
@Autowired(required = false)
    OrderMapper orderMapper;
@Autowired(required = false)
    OrderItemMapper orderItemMapper;
@Autowired(required = false)
    ProductMapper productMapper;
@Autowired(required = false)
    CartMapper cartMapper;
@Autowired
    UserMapper userMapper;
    @Override
    @Transactional
    public void add(OrderDto orderDto) {
        Userentity userentity = userMapper.selectById(orderDto.getUserId());
        BigDecimal jifen=null;
        //扣除积分
        if(orderDto.getJifen()){

            if(userentity.getGouwu().compareTo(orderDto.getPrice())>=0){
                jifen=orderDto.getPrice();
            }else{
                jifen=userentity.getGouwu();
            }

            Userentity userentity1 = new Userentity();
            userentity1.setId(orderDto.getUserId());
            userentity1.setGouwu(userentity.getGouwu().subtract(jifen));
            System.out.println("=======================");
            userMapper.updateByPrimaryKeySelective(userentity1);
        }else{
            jifen= new BigDecimal(0);
        }
        userentity = userMapper.selectById(orderDto.getUserId());
        Orderentity orderentity = new Orderentity();
        BeanUtils.copyProperties(orderDto,orderentity);
        System.out.println(orderentity);
        orderentity.setPrice(orderDto.getPrice().subtract(jifen));
        orderMapper.insert(orderentity);
        List<OrderItemDto> list = orderDto.getList();
        BigDecimal multiply = (orderDto.getPrice().subtract(jifen)).multiply(BigDecimal.valueOf(0.1));
        BigDecimal gouwu = userentity.getGouwu();
        Userentity userentity1 = new Userentity();
        userentity1.setId(orderDto.getUserId());
        userentity1.setGouwu(gouwu.add(multiply));
        userMapper.updateByPrimaryKeySelective(userentity1);
        userMapper.addTotall(orderDto.getUserId(),multiply);
        for (OrderItemDto orderItemDto : list) {
            //新增订单item
            OrderItementity orderItementity = new OrderItementity();
            BeanUtils.copyProperties(orderItemDto,orderItementity);
            orderItementity.setOrderId(orderentity.getId());
            orderItemMapper.insert(orderItementity);
            Productentity byId = productMapper.getById(orderItemDto.getProductId());
            Integer kucun = byId.getKucun();
            Integer xiaoliang = byId.getXiaoliang();
            //减少库存
            Productentity productentity = new Productentity();
            productentity.setId(orderItemDto.getProductId());
            productentity.setKucun(kucun-orderItemDto.getCount());
            productentity.setXiaoliang(xiaoliang+orderItemDto.getCount());

            productMapper.updateByPrimaryKeySelective(productentity);
            //删除购物车
            cartMapper.deleteCart(orderItemDto.getProductId(),orderDto.getUserId());
        }
    }

    @Override
    public PageInfo list(OrderListDto dto) {
        PageHelper.startPage(dto.getPage(),dto.getSize());
        List<OrderListVo> list= orderMapper.selectList(dto);
        System.out.println(list.size());
        for (OrderListVo orderListVo : list) {
            List<OrderItemListVo> vo= orderItemMapper.select(orderListVo.getId());
            orderListVo.setList(vo);
        }
        PageInfo<OrderListVo> userentityPageInfo = new PageInfo<OrderListVo>(list);
        return userentityPageInfo;
    }

    @Override
    public void changeStatus(Integer id, Integer status) {
        Orderentity orderentity = new Orderentity();
        orderentity.setStatus(String.valueOf(status));
        orderentity.setId(id);
        orderMapper.updateByPrimaryKeySelective(orderentity);
    }

    @Override
    public void update(Orderentity orderentity) {
        orderMapper.updateByPrimaryKeySelective(orderentity);
    }
}




